<?php
/**
 * 73YC 发现列表页
 * ============================================================================
 * * 版权所有 20013-* 梅州络客网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.alork.com；
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和
 * 使用；不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * $Author: liubo $
 * $Id: index.php 17217 2013-10-19 06:29:08Z liubo $
*/
define('IN_ECS', true);

require(dirname(__FILE__) . '/includes/init.php');

if ((DEBUG_MODE & 2) != 2)
{
    $smarty->caching = true;
}

$string = base64_decode('YTozOntzOjg6ImtleXdvcmRzIjtzOjY6IumprOWPryI7czoxMDoiaW1hZ2VGaWVsZCI7czowOiIiO3M6MTg6InNlYXJjaF9lbmNvZGVfdGltZSI7aToxNDExMzY5OTAyO30=');
$act=!empty($_GET['act'])?$_GET['act']:'nav';
$cache_id = sprintf('%X', crc32("found.dwt" . '-' .$act. '-' . $_CFG['lang']));

/*用户点击喜欢*/
if ($act == "like") {
    require(dirname(__FILE__) . '/includes/cls_json.php');
    $json = new json;
    $id   = !empty($_POST['id'])?$_POST['id']:0;
    if (empty($id)) {
        exit();
    }
    /*更新点击数*/
    $sql = "UPDATE " . $ecs->table('tvd') . " SET click = (click+1) WHERE tvd_id='$id'";
    $db->query($sql);
    /*判断是否要添加到收藏*/
    if (!empty($_SESSION['user_id'])) {
    $user_id = $_SESSION['user_id'];
    $sql = "SELECT id FROM ". $ecs->table('tvd_collect') . " WHERE  user_id = $user_id AND tvd_id = $id";
    $is_collect = $db->getOne($sql);
        if (empty($is_collect)) {
           $sql = "INSERT INTO " . $ecs->table('tvd_collect') . " (user_id, tvd_id) " .
                    "VALUES('$user_id', '$id')";
           $db->query($sql);
        }
    }
    exit();
}
/*发现：四大栏目*/
if ($act == 'nav') {
    $num = !empty($_GET['num'])?intval($_GET['num']):'1';
    if (!empty($num)) {

        switch ($num) {
            case '1':
                $ad_1=get_ad(39);//首页轮播广告
                $topic = get_topic();
                $smarty->assign('topic', $topic);
                $smarty->assign('ad_1', $ad_1);
                break;
            case '2':
                $topic = get_topic(5,1,2);
                $smarty->assign('topic', $topic);
                break;
            case '3':
                $topic = get_topic_my();
                $smarty->assign('topic', $topic);
                break;
            case '4':
                $content = get_day();
                $day=date("d",time());
                if ($_SESSION['user_id']) {
                    $keys= $_SESSION['user_id'].$day;
                    $gua = $mem->get($keys);
                    $smarty->assign('gua', $gua);
                }
                $smarty->assign('content', $content);
                break;


            default:
                # code...
                break;
        }
    }
    
    $smarty->assign('num', $num);
}
/*翻页*/
if ($act == 'page') {
    require(dirname(__FILE__) . '/includes/cls_json.php');
    $json = new json;
    $num = !empty($_GET['num'])?intval($_GET['num']):'1';//分类类型
    $page = !empty($_REQUEST['page'])?intval($_REQUEST['page']):'1';//分类类型
    $result =array();
    if (!empty($num)) {

        switch ($num) {
            case '1':

                $topic = get_topic(5,$page);
                $smarty->assign('topic', $topic);
                break;
            case '2':
                $topic = get_topic(5,$page,2);
                $smarty->assign('topic', $topic);

                break;
            case '3':
                $topic = get_topic_my(5,$page);
                $smarty->assign('topic', $topic);
                break;
            default:
                # code...
                break;
        }
    $page_num = ($page<10)?"0".$page:$page;
    $smarty->assign('num', $num);
    $smarty->assign('page', $page_num);
    if (!empty($topic)) {
        $result['error']   = 1;
        $result['page']    = $page;
        $result['content'] = $smarty->fetch("./library/found_page.lbi");
    }
    else{
        $result['error']   = 2;
    }



    die($json->encode($result));


    }

}
/*挂奖*/
if ($act == 'gua') {
    require(dirname(__FILE__) . '/includes/cls_json.php');
    $json = new json;
    if (!empty($_SESSION['user_id'])) {
        $day    = date("d",time());
        $key    = $_SESSION['user_id'].$day;
        $keys   = $mem->add($key,1,86400);
        die($json->encode($keys));
    }
    else{
        exit();
    }
}







    $smarty->assign('found', 1);
    $smarty->display("found.dwt");













/**
 * 获取话题
 *
 * @access  private
 * @param   int     $size  //调取数量
 * @param   int     $type  //1为日日干货 2为涨知识
 * @param   int     $page  //页码
 * @return  array
 */
function get_topic($size = 5,$page = 1,$num = 1){
    $where = "";
    if ($num == 2) {
       $where = " AND t.ext_type = 1";
    }
    if ($_SESSION['user_id']) {
    $user_id = $_SESSION['user_id'];
    $sql =  " SELECT t.list_img ,t.tvd_id,t.title,t.title_small,t.ext_img,t.click,tc.id,c.cat_name FROM ". $GLOBALS['ecs']->table('tvd') ." AS t".
            " LEFT JOIN ".$GLOBALS['ecs']->table('tvd_collect')." AS tc ON t.tvd_id = tc.tvd_id AND user_id = $user_id ".
            " LEFT JOIN ".$GLOBALS['ecs']->table('tvd_cat')." AS c ON c.cat_id = t.cat_id  ".
            " WHERE t.is_open = 1 AND c.cat_id != 55".$where.
            " ORDER BY t.sort DESC,t.add_time DESC";
    }
    else{
    $sql =  " SELECT t.list_img ,t.tvd_id,t.title,t.title_small,t.ext_img,t.click,c.cat_name FROM ". $GLOBALS['ecs']->table('tvd') ." AS t".
            " LEFT JOIN ".$GLOBALS['ecs']->table('tvd_cat')." AS c ON c.cat_id = t.cat_id  ".
            " WHERE t.is_open = 1 AND c.cat_id != 55 ".$where.
            " ORDER BY t.sort DESC,t.add_time DESC";
    }
    $res = $GLOBALS['db']->selectLimit($sql, $size, ($page - 1) * $size);
    /*重组数据*/
    $key = 1;
    while ($row = $GLOBALS['db']->fetchRow($res))
    {
        $arr[$key]['id']          = $row['tvd_id'];
        $arr[$key]['pic']         = $GLOBALS['egg_img'].$row['list_img'];
        $arr[$key]['pic_thumb']   = !empty($row['ext_img'])?$GLOBALS['egg_img'].$row['ext_img']:'';
        $arr[$key]['url']         = "tvd.php?id=".$row['tvd_id'];
        $arr[$key]['title']       = $row['title'];
        $arr[$key]['title_small'] = !empty($row['title_small'])?$row['title_small']:"#".$row['cat_name'];
        $arr[$key]['click']       = $row['click'];
        $arr[$key]['is_collect']  = !empty($row['id'])?1:0;
        $key++;
    }
    return $arr;
}
/**
 * 获取我关注
 *
 * @access  private
 * @param   int     $size  //调取数量
 * @param   int     $page  //页码
 * @return  array
 */
function get_topic_my($size = 5,$page = 1){

    if ($_SESSION['user_id']) {
    $user_id = $_SESSION['user_id'];
    $sql =  " SELECT t.list_img ,t.tvd_id,t.title,t.title_small,t.ext_img,t.ext_img,c.cat_name FROM ". $GLOBALS['ecs']->table('tvd') ." AS t".
            " RIGHT JOIN ".$GLOBALS['ecs']->table('tvd_collect')." AS tc ON t.tvd_id = tc.tvd_id AND user_id = $user_id ".
            " LEFT JOIN ".$GLOBALS['ecs']->table('tvd_cat')." AS c ON c.cat_id = t.cat_id  ".
            " WHERE t.is_open = 1";
    }
    else{
    return false;
    }
    $res = $GLOBALS['db']->selectLimit($sql, $size, ($page - 1) * $size);
    /*重组数据*/
    $key = 1;
    while ($row = $GLOBALS['db']->fetchRow($res))
    {
        $arr[$key]['id']          = $row['tvd_id'];
        $arr[$key]['pic']         = $GLOBALS['egg_img'].$row['list_img'];
        $arr[$key]['pic_thumb']   = !empty($row['ext_img'])?$GLOBALS['egg_img'].$row['ext_img']:'';
        $arr[$key]['title']       = $row['title'];
        $arr[$key]['title_small'] = !empty($row['title_small'])?$row['title_small']:"#".$row['cat_name'];
        $arr[$key]['url']         = "tvd.php?id=".$row['tvd_id'];
        $key++;
    }
    return $arr;
}

/**
 * 获取每日福利
 *
 * @access  private
 * @param   int     $size  //调取数量
 * @param   int     $page  //页码
 * @return  array
 */
function get_day(){
    $sql =  " SELECT content FROM ". $GLOBALS['ecs']->table('tvd') ." WHERE is_open = 1 AND cat_id = 55";
    $arr = $GLOBALS['db']->getOne($sql);
    return $arr;
}

/**
 * 获得广告位
 *
 * @access  private
 * @return  array
 */
    function get_ad($position_id = 0){
        if(!$position_id) return false;
        $position_id = intval($position_id);
        $sql = "SELECT * FROM " . $GLOBALS['ecs']->table('ad') . " WHERE position_id = '$position_id' ORDER BY ad_name ASC ";
        $ad = $GLOBALS['db']->getAll($sql);

        foreach ($ad as $key => $value) {
            $ad[$key]['pic'] = $GLOBALS['egg_img']."data/afficheimg/".$value['ad_code'];
        }
        return $ad;
    }





 ?>